import { usePermissionStore } from "@/store/modules/permission";
const permissionStore = usePermissionStore();
/**
 * Check if an element has a class
 * @param {HTMLElement} ele
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  	return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}

/**
 * Add class to element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  	if (!hasClass(ele, cls)) ele.className += " " + cls;
}

/**
 * Remove class from element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
	if (hasClass(ele, cls)) {
		const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
		ele.className = ele.className.replace(reg, " ");
	}
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
	const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
	return isExternal;
}

/** 查询是否有路由权限
 * @param {string} path
 * @returns {Boolean}
 */
export function routerHandle(str:string,name:string){
	var route = str.split('/').filter(function (s) {
		return s && s.trim();
	});
	var path = `/${route[0]}`;
	var isHave = false;
	console.log(permissionStore.routes)
	console.log(name,path)
	const checkChildren = (children:any)=>{
		if(children){
			children.forEach((item:any)=>{
				if(item.name == name){
					isHave = true;
				}
				checkChildren(item.children);
			})
		}
	}
	permissionStore.routes.forEach((item:any)=>{
		if(item.path == path){
			checkChildren(item.children);
		}
	})
	return isHave;
}